package com.imoblife.brainwavestus.share;


import java.io.IOException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.webkit.WebView;
import android.widget.Toast;

import com.imoblife.brainwavestus.R;
import com.twitterapime.rest.Credential;
import com.twitterapime.rest.TweetER;
import com.twitterapime.rest.UserAccountManager;
import com.twitterapime.search.LimitExceededException;
import com.twitterapime.search.Tweet;
import com.twitterapime.xauth.Token;
import com.twitterapime.xauth.ui.OAuthDialogListener;
import com.twitterapime.xauth.ui.impl.WebViewOAuthDialogWrapper;

/**
 * @author xxpgd2@qq.com
 */
public class TwitterShareActivity extends Activity implements
		OAuthDialogListener {
	/**
	 * 
	 */
	private final String CONSUMER_KEY = "Nxja7McVrDOnXr6NcPPbw";
	private final String CONSUMER_SECRET = "nS4eTY12TbKGvFLiqlzfpnVb1y1lvkXAe7hsjVTE";
	private final String CALLBACK_URL = "https://play.google.com/store/apps/details?id=imoblife.brainwavestus";

	private ProgressDialog dialog;
	private WebViewOAuthDialogWrapper pageWrapper;

	/**
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(android.R.style.Theme_Light_NoTitleBar_Fullscreen);
		dialog = new ProgressDialog(TwitterShareActivity.this);
		dialog.setCanceledOnTouchOutside(false);
		dialog.requestWindowFeature(android.R.style.Theme_Light_NoTitleBar_Fullscreen);
		dialog.setMessage("Loading...");
		dialog.show();
		WebView webView = new WebView(this);
		setContentView(webView);
		initWebViewOAuthDialogWrapper(webView, dialog);

	}

	
	protected void onDestroy() {
		
		super.onDestroy();
		dialog.dismiss();
	}

	private void initWebViewOAuthDialogWrapper(WebView webView,
			ProgressDialog dialog) {

		pageWrapper = new WebViewOAuthDialogWrapper(webView, dialog);
		//
		pageWrapper.setConsumerKey(CONSUMER_KEY);
		pageWrapper.setConsumerSecret(CONSUMER_SECRET);
		pageWrapper.setCallbackUrl(CALLBACK_URL);
		pageWrapper.setOAuthListener(this);
		//
		pageWrapper.login();
	}

	/**
	 * @see com.twitterapime.xauth.ui.OAuthDialogListener#onAuthorize(com.twitterapime.xauth.Token)
	 */
	public void onAuthorize(Token accessToken) {
		Credential c = new Credential(CONSUMER_KEY, CONSUMER_SECRET,
				accessToken);
		final UserAccountManager uam = UserAccountManager.getInstance(c);
		//
		try {
			if (uam.verifyCredential()) {

				String str = getIntent().getStringExtra("share_title")+ " "
						+ getIntent().getStringExtra("share_content") + " "
						+ getIntent().getStringExtra("share_url") + " "
						+ getIntent().getStringExtra("share_extr");

				if (str.length() > 140) {
					System.out.println("*****Titter���ݳ��Ȳ��ܳ���140���ַ����޸ĺ����ԡ�");
					TwitterShareActivity.this.finish();
				}
				System.out.println(str);

				Tweet t = new Tweet(str);

				TweetER ter = TweetER.getInstance(uam);
				try {
					t = ter.post(t);
					System.out.println("*****post");
					showToast(getResources().getString(R.string.share_success));
				} catch (IOException e) {
					System.out.println("*****IOException");
					showToast(getResources().getString(R.string.share_fail));
					e.printStackTrace();

				} catch (LimitExceededException e) {
					System.out.println("*****LimitExceededException");
					showToast(getResources().getString(R.string.share_fail));
					e.printStackTrace();
				} catch (SecurityException e) {
					System.out.println("*****SecurityException");
					showToast(getResources().getString(
							R.string.security_exception));
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					System.out.println("*****IllegalArgumentException");
					showToast(getResources().getString(R.string.error));
					e.printStackTrace();
				} finally {
					TwitterShareActivity.this.finish();
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("*****Exception");
			showToast(getResources().getString(R.string.error));
		}
	}

	/**
	 * @see com.twitterapime.xauth.ui.OAuthDialogListener#onAccessDenied(java.lang.String)
	 */
	public void onAccessDenied(String message) {
		System.out.println("*****onAccessDenied");
		showToast(getResources().getString(R.string.security_exception));
	}

	/**
	 * @see com.twitterapime.xauth.ui.OAuthDialogListener#onFail(java.lang.String,
	 *      java.lang.String)
	 */
	public void onFail(String error, String message) {
		System.out.println("*****onFail");
		showToast(getResources().getString(R.string.security_exception));
	}

	private void showToast(final String message) {
		new Thread() {
			public void run() {
				Log.i("log", "run");
				Looper.prepare();
				Toast.makeText(TwitterShareActivity.this, message,
						Toast.LENGTH_LONG).show();
				Looper.loop();// ����loop�е�ѭ�����鿴��Ϣ����
			}
		}.start();
		TwitterShareActivity.this.finish();
	}

}
